var htmlone = "";
var liheight = [];
var issame = "", bool;
$.ajax({
    url: "http://chst.vip:1234/api/getbrandtitle",
    type: "Get",
    async: false,

}).then(res => {
    // console.log(res);
    res.result.forEach(item => {
        htmlone += `
        <li><div class="box"><span>${item.brandTitle}</span><div class="log">▼</div></div><ul class="titletwo"></ul></li>
        `;
    });
    $("main>ul.titleone").html(htmlone);
    Array.from($("main>ul.titleone>li")).forEach((item, index) => {

        $.ajax({
            url: "http://chst.vip:1234/api/getbrand",
            type: "Get",
            async: false,
            data: {
                brandtitleid: index
            },
            success(suc) {
                // console.log(suc);
                var htmltwo = "";
                suc.result.forEach((item, index) => {
                    htmltwo += `
                        <li brandtitleid=${item.brandTitleId}>
                        <span>${index + 1}</span>
                        <b>${item.brandName}</b>
                        <br>
                        <p>${item.brandInfo}</p>
                        </li>
                    `
                })
                $($(item).children()[1]).html(htmltwo);
            }
        })
        Array.from($("main>ul>li")).forEach((item, index) => {
            liheight[index] = $(item).height();
        })
    })
    startheight();
    //console.log($("main>ul>li>ul>li"));
    $("main>ul>li>.box").click(function () {
        var index = $("main>ul>li").index($(this).parent());
        //console.log(issame,this);
        index = liheight[index] + "px"
        if (issame === this) {
            //console.log("点击的是同一个元素");
            if (bool) {
                // console.log("这个元素时打开的",bool);
                $(this).parent().animate({
                    height: index
                })
                bool = !bool;
            } else {
                // console.log("这个元素是关闭的",bool);
                $(this).parent().animate({
                    height: "1.002667rem"
                })
                bool = !bool;
            }
            issame = this;
        } else {
            // console.log("点击的不是同一个元素");
            $(this).parent().siblings().animate({
                height: "1.002667rem"
            }).end().animate({
                height: index
            })
            bool = false;
            issame = this;
        }
    })
    $("main>ul>li>.titletwo>li").click(function () {
        // console.log($(this));
        let brandid=$(this).attr("brandtitleid");
        // console.log($(this).attr("brandid"));
        let title=$($(this).children()[1]).html();
        // console.log(title);
        // console.log(brandid);
        location.href=`./brand.html?title=${title}&brandtitleid=${brandid}`
    })

})
function startheight() {
    $("main>ul>li").css({
        height: "1.002667rem"
    })
}